const ResponseModel = require('../models/response-model')
const SystemConfig = require('../utils/system-config')
const TorrentModel = require('../models/torrent-model')
const TMDB = require('../utils/media-netdata/tmdb');

const MediaService = require('../services/media-service')

const cache = require('../utils/cache');

module.exports = {
    // 
    'get::/media/search': async (ctx) => {
        // console.log(systemConfig);
        const { keyword } = ctx.request.query;
        const cacheKey = `media-search-${keyword}`;
        let data = cache.get(cacheKey);
        if (!data) {
            data = await TMDB.search(keyword)
            data.length && cache.set(cacheKey, data, 24*60*60)
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 
    'get::/media/popular/tv': async (ctx) => {
        // console.log(systemConfig);
        const cacheKey = 'media-popular-tv';
        let data = cache.get(cacheKey);
        if (!data) {
            data = await TMDB.tvPopular()
            data.length && cache.set(cacheKey, data, 24*60*60)
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 
    'get::/media/popular/movie': async (ctx) => {
        // console.log(systemConfig);
        const  cacheKey = 'media-popular-movie';
        let data = cache.get(cacheKey);
        if (!data) {
            data = await TMDB.moviePopular()
            data.length && cache.set(cacheKey, data, 24*60*60)
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 正在播放的电视剧
    'get::/media/now/tv': async (ctx) => {
        // console.log(systemConfig);
        const  cacheKey = 'media-now-tv';
        let data = cache.get(cacheKey);
        if (!data) {
            data = await TMDB.tvOnTheAir()
            data.length && cache.set(cacheKey, data, 24*60*60)
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 上映中的电影
    'get::/media/now/movie': async (ctx) => {
        // console.log(systemConfig);
        const  cacheKey = 'media-now-movie';
        let data = cache.get(cacheKey);
        if (!data) {
            data = await TMDB.movieNowPlaying()
            data.length && cache.set(cacheKey, data, 24*60*60)
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 
    'get::/media/getByTmdbid': async (ctx) => {
        const { tmdbid, type } = ctx.request.query;
        let data;
        if (tmdbid) {
            data = await MediaService.getMedia(tmdbid, type, false)
        }
        ctx.response.body = ResponseModel.success(data)
    },
}
  